
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>Angular2使用笔记8--在Angular2中使用Observable对象 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="最近在学习Angular2作为前端的框架，《Angular2使用笔记》系列用于记录过程中的一些使用和解决方法。本文介绍Observor、Subject相关，以及在Angular2中Http服务以及Observable对象的使用。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="https://godbasin.github.io/front-end-playground">前端游乐场</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/11/06/angular2-notes-8-rxjs-in-angular2/" title="Angular2使用笔记8--在Angular2中使用Observable对象" itemprop="url">Angular2使用笔记8--在Angular2中使用Observable对象</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2016-11-06T01:46:47.000Z" itemprop="datePublished">2016-11-06</time>
    更新日期:<time datetime="2018-12-18T15:30:23.868Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#认识Rxjs"><span class="toc-number">1.</span> <span class="toc-text">认识Rxjs</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Rxjs基本概念"><span class="toc-number">1.1.</span> <span class="toc-text">Rxjs基本概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Observable-可观察对象"><span class="toc-number">1.2.</span> <span class="toc-text">Observable(可观察对象)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Observable四大核心"><span class="toc-number">1.3.</span> <span class="toc-text">Observable四大核心</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用Observable"><span class="toc-number">2.</span> <span class="toc-text">使用Observable</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#创建Other组件"><span class="toc-number">2.1.</span> <span class="toc-text">创建Other组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建获取数据的Http服务"><span class="toc-number">2.2.</span> <span class="toc-text">创建获取数据的Http服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在Other组件中注入相关服务并使用"><span class="toc-number">2.3.</span> <span class="toc-text">在Other组件中注入相关服务并使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#倒退为承诺-Promise"><span class="toc-number">2.4.</span> <span class="toc-text">倒退为承诺(Promise)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>最近在学习Angular2作为前端的框架，《Angular2使用笔记》系列用于记录过程中的一些使用和解决方法。<br>本文介绍Observor、Subject相关，以及在Angular2中Http服务以及Observable对象的使用。<br><a id="more"></a></p>
<h2 id="认识Rxjs"><a href="#认识Rxjs" class="headerlink" title="认识Rxjs"></a>认识Rxjs</h2><hr>
<p>可能比较多的java小伙伴会比较熟悉吧，rx这个东西。不知不觉前端又把各种精华吸取进来了呢。哈哈。</p>
<h3 id="Rxjs基本概念"><a href="#Rxjs基本概念" class="headerlink" title="Rxjs基本概念"></a>Rxjs基本概念</h3><ul>
<li>Observable(可观察对象) : 是一个包含来自未来、可以被使用的值(value)或事件(event)的集合</li>
<li>Observe(观察者)：是一个知道如何监听、处理来自Obervable的值的函数集合 </li>
<li>Subscription(订阅)：代表着Observable的执行动作，我们可以使用它来停止Obervable继续执行</li>
<li>Operators(操作)：一系列可以操作集合的pure function，像是过滤(filter)、转换(map)等等</li>
<li>Subject：相当于一个事件发射器，是唯一能够向多个Observer广播值(value)的唯一手段</li>
<li>Schedulers(调度)：是一个中央调度员，帮助我们控制并发，协调计算(setTimeout、requestAnimationFrame等)</li>
</ul>
<p>这里我们重点介绍一下Observable和Subject，其他的Observer、Subscription等都相对比较简单。</p>
<h3 id="Observable-可观察对象"><a href="#Observable-可观察对象" class="headerlink" title="Observable(可观察对象)"></a>Observable(可观察对象)</h3><p>Observable（可观察对象）是基于推送（Push）运行时执行（lazy）的多值集合。</p>
<ul>
<li><p>拉取(Pull)和推送(Push)<br>拉取和推送是数据生产者和数据消费者之间通信的两种不同机制。</p>
<ul>
<li>拉取：在拉取系统中，总是由消费者决定何时从生产者那里获得数据。生产者对数据传递给消费者的时间毫无感知（被动的生产者，主动的消费者）</li>
<li>推送：在推送系统中生产者决定何时向消费者传递数据，消费者对何时收到数据毫无感知（被动的消费者）</li>
</ul>
</li>
<li><p>js中的Promise和Observable</p>
<ul>
<li>现代JavaScript中Promise是典型的推送系统。作为数据生产者的Promise通过resolve()向数据消费者——回调函数传递数据：与函数不同，Promise决定向回调函数推送值的时间</li>
<li>RxJS在JavaScript中引入了Observable(可观察对象)这个新的推送系统。Observable是多数据值的生产者，向Observer(被动的消费者)推送数据</li>
</ul>
</li>
<li><p>Observable与函数、promsise</p>
<ul>
<li>函数是当调用才同步计算，并最终只返回一个值的</li>
<li>promise是会或者不会返回一个值</li>
<li>Observable是当调用才同步或者异步地计算，并可能产生0到无穷多个值的</li>
</ul>
</li>
<li><p>Observable是函数概念的拓展</p>
<ul>
<li>Observable就像一个没有参数的函数，并不断生成一些值供我们使用，因此它也像是一个事件发射机(EventEmitters)</li>
<li>在Observable中subscribe就像call一个函数，你订阅它，它才会被’启动’。同一个Observable对于不同的subscribe，是不会共享结果的(通常情况下这样子的，但可以通过调用api来共享)</li>
</ul>
</li>
</ul>
<h3 id="Observable四大核心"><a href="#Observable四大核心" class="headerlink" title="Observable四大核心"></a>Observable四大核心</h3><ul>
<li>创建<ul>
<li>Rx.Observable.create是Observable构造函数的别名，接受一个参数：subscribe函数</li>
<li>除了使用create创建Observable，我们通常还使用创建操作符, 如of，from，interval,等来创建Observable</li>
</ul>
</li>
<li>订阅<ul>
<li>observable.subscribe和Observable.create(function subscribe(observer) {…})中的subscribe不是同一个对象，但在工程中可以在概念上视两者为等价物</li>
<li>调用subscribe的观察者并不会共享同一个Observable</li>
<li>订阅机制与处理事件的addEventListener/removeEventListenerAPI完全不同。通过observable.subscribe，观察者并不需要在Observable中进行注册，Observable也不需要维护订阅者的列表</li>
<li>订阅后便进入了Observable的执行阶段，在执行阶段值和事件将会被传递给观察者供其消费</li>
</ul>
</li>
<li>执行<ul>
<li>只有在被订阅之后Observable才会执行，执行的逻辑在Observable.create(function subscribe(observer){…})中描述，执行后将会在特定时间段内，同步或者异步地成产多个数据值</li>
<li>Observable在执行过程中，可以推送三种类型的值：<ul>
<li>“Next” 通知： 实际产生的数据，包括数字、字符串、对象等</li>
<li>“Error” 通知：一个JavaScript错误或者异常</li>
<li>“Complete” 通知：一个不带有值的事件</li>
</ul>
</li>
<li>在Observable的执行过程中，0个或者多个“Next”通知会被推送</li>
<li>在错误或者完成通知被推送后，Observable不会再推送任何其他通知</li>
</ul>
</li>
<li>终止<ul>
<li>Observable的执行可能是无限的，作为观察者需要主动中断执行：我们需要特定的API去终止执行过程</li>
<li>因为特定的观察者都有特定的执行过程，一旦观察者获得想要的数据后就需要终止执行过程以免带来计算时对内存资源的浪费</li>
<li>在observable.subscribe被调用时，观察者会与其执行作用域绑定，同时返回一个Subscription类型的对象，通过调用subscription.unsubscribe()你可以终止执行过程</li>
</ul>
</li>
</ul>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><p>对于Observable和Subject的理解很多时候我也是从网上读到的呢，所以可能本篇博客相对来说粘贴别人的东西会比较多吧。<br><a href="https://segmentfault.com/a/1190000005051034" target="_blank" rel="external">《RxJs 核心概念之Observable》</a><br><a href="https://zhuanlan.zhihu.com/p/20531896" target="_blank" rel="external">《不要把Rx用成Promise》</a><br><a href="https://github.com/Reactive-Extensions/RxJS/tree/master/doc/api" target="_blank" rel="external">RxJS4.0</a></p>
<h2 id="使用Observable"><a href="#使用Observable" class="headerlink" title="使用Observable"></a>使用Observable</h2><hr>
<p>这里我们创建一个Other页面组件，用于展示通过Angular的Http服务返回的Observable对象中获取的数据。</p>
<h3 id="创建Other组件"><a href="#创建Other组件" class="headerlink" title="创建Other组件"></a>创建Other组件</h3><ul>
<li>在app.routes.ts文件中添加该页面路由</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> routes: RouterConfig = [</div><div class="line">  ... <span class="comment">// 其他路由</span></div><div class="line">  &#123; path: <span class="string">'other'</span>,  component: Other &#125;</div><div class="line">];</div></pre></td></tr></table></figure>
<ul>
<li>新建other文件夹</li>
<li>添加组件模板other.template.ts</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">&lt;!--插入头部组件，注入指令后生效--&gt;</div><div class="line">&lt;my-header&gt;&lt;/my-header&gt;</div><div class="line">&lt;div class="container"&gt;</div><div class="line">    &lt;h3&gt;RxJS相关&lt;/h3&gt;</div><div class="line">    &lt;table class="table"&gt;</div><div class="line">        &lt;tbody&gt;</div><div class="line">            &lt;tr *ngFor="let item of rxjsDate"&gt;</div><div class="line">                &lt;td&gt;</div><div class="line">                    &lt;h4&gt;&#123;&#123; item.name &#125;&#125; : &lt;/h4&gt;</div><div class="line">                    &lt;h5&gt;&#123;&#123; item.content &#125;&#125;&lt;/h5&gt;</div><div class="line">                &lt;/td&gt;</div><div class="line">            &lt;/tr&gt;</div><div class="line">        &lt;/tbody&gt;</div><div class="line">    &lt;/table&gt;</div><div class="line">&lt;/div&gt;</div></pre></td></tr></table></figure>
<ul>
<li>other文件夹下index.ts中输出组件</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123; Component &#125; from <span class="string">'@angular/core'</span>;</div><div class="line"><span class="comment">// 添加Header组件，默认从header文件夹的index.ts中获取</span></div><div class="line"><span class="keyword">import</span> &#123; Header &#125; from <span class="string">'../header'</span>;</div><div class="line"></div><div class="line">@Component(&#123;</div><div class="line">  selector: <span class="string">'other'</span>, <span class="comment">// 设置模板元素</span></div><div class="line">  templateUrl: <span class="string">'./other.template.html'</span>, <span class="comment">// 模板文件引入</span></div><div class="line">  directives: [Header] <span class="comment">// 注入指令</span></div><div class="line">&#125;)</div><div class="line"><span class="keyword">export</span> <span class="keyword">class</span> Other &#123;</div><div class="line">  <span class="comment">// 定义并初始化数据</span></div><div class="line">  rxjsDate: <span class="built_in">Array</span>&lt;<span class="built_in">any</span>&gt; = [];</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="创建获取数据的Http服务"><a href="#创建获取数据的Http服务" class="headerlink" title="创建获取数据的Http服务"></a>创建获取数据的Http服务</h3><p>这里我们把该服务写在other.service.ts文件中。<br><figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 获取Injectable服务</span></div><div class="line"><span class="keyword">import</span> &#123; Injectable &#125; from <span class="string">'@angular/core'</span>;</div><div class="line"><span class="comment">// 获取Http服务</span></div><div class="line"><span class="keyword">import</span> &#123; Http, Response &#125; from <span class="string">'@angular/http'</span>;</div><div class="line"><span class="comment">// 获取Observable服务</span></div><div class="line"><span class="keyword">import</span> &#123; Observable &#125; from <span class="string">'rxjs/Observable'</span>;</div><div class="line"><span class="comment">// 获取rxjs相关操作服务（map等）,后面我们会提到</span></div><div class="line"><span class="keyword">import</span> <span class="string">'../rxjs-operators'</span>;</div><div class="line"></div><div class="line">@Injectable()</div><div class="line"><span class="keyword">export</span> <span class="keyword">class</span> OtherService &#123;</div><div class="line">  <span class="comment">// 注入Http服务</span></div><div class="line">  <span class="keyword">constructor</span>(public http: Http) &#123;&#125;</div><div class="line">  <span class="comment">// 设置获取数据的地址，这里我们使用本地的json文件模拟</span></div><div class="line">  <span class="keyword">private</span> dataUrl: <span class="built_in">string</span> = <span class="string">'app/info.json'</span>;</div><div class="line"></div><div class="line">  <span class="comment">// 定义方法，用于获取Observable服务</span></div><div class="line">  getDatas(): Observable&lt;<span class="built_in">any</span>&gt; &#123;</div><div class="line">    <span class="comment">// 使用angular的http服务获取数据，默认返回observable</span></div><div class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.http.get(<span class="keyword">this</span>.dataUrl)</div><div class="line">      <span class="comment">// 响应数据是JSON字符串格式的。 我们必须把这个字符串解析成JavaScript对象</span></div><div class="line">      .map(res =&gt; res.json().data)</div><div class="line">      <span class="comment">// 异常的捕获并进行处理</span></div><div class="line">      .catch(<span class="keyword">this</span>.handleError);</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// 定义私有方法来处理异常</span></div><div class="line">  <span class="keyword">private</span> handleError(error: <span class="built_in">any</span>) &#123;</div><div class="line">    <span class="comment">// 我们的服务处理器(handleError)把响应对象记录到控制台中</span></div><div class="line">    <span class="comment">// 把错误转换成对用户友好的消息，并且通过Observable.throw来</span></div><div class="line">    <span class="comment">// 把这个消息放进一个新的、用于表示“失败”的可观察对象</span></div><div class="line">    <span class="keyword">let</span> errMsg = (error.message) ? error.message :</div><div class="line">      error.status ? <span class="string">`<span class="subst">$&#123;error.status&#125;</span> - <span class="subst">$&#123;error.statusText&#125;</span>`</span> : <span class="string">'Server error'</span>;</div><div class="line">    <span class="built_in">console</span>.error(errMsg); <span class="comment">// 输出异常信息</span></div><div class="line">    <span class="keyword">return</span> Observable.throw(errMsg);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<ul>
<li>关于rxjs相关操作符<br>RxJS(“Reactive Extensions”的缩写)是一个被Angular认可的第三方库，它实现了异步可观察对象(asynchronous observable)模式。</li>
</ul>
<p>Angular在rxjs/Observable模块中导出了一个精简版的Observable 类，这个版本缺少很多操作符。</p>
<p>我们将一个一个的导入Observable的操作符和静态类方法，直到我们得到了一个精确符合我们需求的自定义Observable实现。 我们将把这些import语句放进一个app/rxjs-operators.ts文件里。<br><figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// import 'rxjs/Rx'; </span></div><div class="line"><span class="comment">// adds ALL RxJS statics &amp; operators to Observable</span></div><div class="line"></div><div class="line"><span class="comment">// See node_module/rxjs/Rxjs.js</span></div><div class="line"><span class="comment">// Import just the rxjs statics and operators we need for THIS app.</span></div><div class="line"></div><div class="line"><span class="comment">// Statics</span></div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/observable/throw'</span>;</div><div class="line"></div><div class="line"><span class="comment">// Operators</span></div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/catch'</span>;</div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/debounceTime'</span>;</div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/distinctUntilChanged'</span>;</div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/map'</span>;</div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/switchMap'</span>;</div><div class="line"><span class="keyword">import</span> <span class="string">'rxjs/add/operator/toPromise'</span>;</div></pre></td></tr></table></figure></p>
<h3 id="在Other组件中注入相关服务并使用"><a href="#在Other组件中注入相关服务并使用" class="headerlink" title="在Other组件中注入相关服务并使用"></a>在Other组件中注入相关服务并使用</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">... <span class="comment">// 添加其他服务</span></div><div class="line"></div><div class="line"><span class="comment">// 添加OtherService服务，用来获取数据</span></div><div class="line"><span class="keyword">import</span> &#123; OtherService &#125; from <span class="string">'./other.service'</span>;</div><div class="line"></div><div class="line">@Component(&#123;</div><div class="line">  ... <span class="comment">// 其他元数据</span></div><div class="line">  providers:[OtherService] <span class="comment">// 实例化服务</span></div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">class</span> Other &#123;</div><div class="line">  ...</div><div class="line"></div><div class="line">  <span class="comment">// 注入服务</span></div><div class="line">  <span class="keyword">constructor</span>(private otherService: OtherService) &#123;&#125;</div><div class="line"></div><div class="line">  ngOnInit() &#123;</div><div class="line">    <span class="comment">// 获得Obervable对象并进行订阅</span></div><div class="line">    <span class="keyword">this</span>.otherService.getDatas().subscribe(</div><div class="line">      <span class="comment">// 获取数据并保存在this.rxjsDate中</span></div><div class="line">      datas =&gt; <span class="keyword">this</span>.rxjsDate = datas,</div><div class="line">      <span class="comment">// 获取错误信息并保存在this.errorMessage中</span></div><div class="line">      error =&gt; <span class="keyword">this</span>.errorMessage = &lt;<span class="built_in">any</span>&gt;error);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>页面如下：<br><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/E597.tmp.png" alt="image"></p>
<h3 id="倒退为承诺-Promise"><a href="#倒退为承诺-Promise" class="headerlink" title="倒退为承诺(Promise)"></a>倒退为承诺(Promise)</h3><p>Angular的http客户端API返回的是Observable<response>类型的对象，但我们也可以把它转成Promise。</response></p>
<p>这很容易，并且在简单的场景中，一个基于承诺(Promise)的版本看起来很像基于可观察对象(Observable)的版本。</p>
<ul>
<li>把可观察对象转变成承诺，调用toPromise(success, fail)</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">return</span> <span class="keyword">this</span>.http.get(<span class="keyword">this</span>.dataUrl)</div><div class="line">       .toPromise()</div><div class="line">       .then(<span class="keyword">this</span>.extractData)</div><div class="line">       .catch(<span class="keyword">this</span>.handleError);</div></pre></td></tr></table></figure>
<ul>
<li>对调用方组件进行调整，让它期待一个Promise而非Observable，调用这个返回的承诺的then方法，而不再是subscribe</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">this</span>.otherService.getDatas().then(</div><div class="line">     datas =&gt; <span class="keyword">this</span>.rxjsDate = datas,</div><div class="line">     error =&gt; <span class="keyword">this</span>.errorMessage = &lt;<span class="built_in">any</span>&gt;error);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>从刚开始接触http服务，到ajax，然后是Promise，如今发现还有个Observable。<br>不得不说，前端发展真的很快呢，不断地幻化，然后大步往前。这种不断自我演进的属性真的捕获了本骚年的心呢。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-notes/8-rxjs-in-angular2" target="_blank" rel="external">此处查看项目代码（仅包含src部分）</a><br><a href="http://angular2-notes.godbasin.com/8-rxjs-in-angular2/index.html#/other" target="_blank" rel="external">此处查看页面效果</a></p>
  
		
		<p style="margin-top:50px;">
			查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a>
			<br>
			更欢迎来<a href="https://godbasin.github.io/front-end-playground">被删的前端游乐场</a>边撸猫边学前端噢
		</p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/angular2火锅/">angular2火锅</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2016/11/12/angular2-notes-9-websocket-service-with-rxjs/" title="Angular2使用笔记9--使用Subject创建Websocket服务">
  <strong>PREVIOUS:</strong><br/>
  <span>
  Angular2使用笔记9--使用Subject创建Websocket服务</span>
</a>
</div>


<div class="next">
<a href="/2016/11/05/angular2-notes-7-basic-concept-in-angular2/"  title="Angular2使用笔记7--Angular2中的基础概念">
 <strong>NEXT:</strong><br/> 
 <span>Angular2使用笔记7--Angular2中的基础概念
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#认识Rxjs"><span class="toc-number">1.</span> <span class="toc-text">认识Rxjs</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Rxjs基本概念"><span class="toc-number">1.1.</span> <span class="toc-text">Rxjs基本概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Observable-可观察对象"><span class="toc-number">1.2.</span> <span class="toc-text">Observable(可观察对象)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Observable四大核心"><span class="toc-number">1.3.</span> <span class="toc-text">Observable四大核心</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用Observable"><span class="toc-number">2.</span> <span class="toc-text">使用Observable</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#创建Other组件"><span class="toc-number">2.1.</span> <span class="toc-text">创建Other组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建获取数据的Http服务"><span class="toc-number">2.2.</span> <span class="toc-text">创建获取数据的Http服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#在Other组件中注入相关服务并使用"><span class="toc-number">2.3.</span> <span class="toc-text">在Other组件中注入相关服务并使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#倒退为承诺-Promise"><span class="toc-number">2.4.</span> <span class="toc-text">倒退为承诺(Promise)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/15/wxapp-latest-20191115/" title="小程序开发月刊第11期（20191115）">小程序开发月刊第11期（201911...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/10/change-log/" title="前端 CHANGELOG 生成指南">前端 CHANGELOG 生成指南...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/09/wxapp-global-data-behavior/" title="小程序的奇技淫巧 -- globalDataBehavior管理全局状态">小程序的奇技淫巧 -- global...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/15/wxapp-latest-20191015/" title="小程序开发月刊第十期（20191015）">小程序开发月刊第十期（2019101...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/13/about-front-end-3-growth/" title="前端这几年--3.关于成长和焦虑">前端这几年--3.关于成长和焦虑...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/09/16/wxapp-latest-20190916/" title="小程序开发月刊第九期（20190916）">小程序开发月刊第九期（2019091...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/08/15/wxapp-latest-20190815/" title="小程序开发月刊第八期（20190815）">小程序开发月刊第八期（2019081...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/27/front-end-playground/" title="被删的前端游乐场建成！">被删的前端游乐场建成！</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/21/vue-for-everyone-3/" title="9102全员学Vue--3.把页面拼成个Web应用">9102全员学Vue--3.把页面拼...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/15/wxapp-latest-20190715/" title="小程序开发月刊第七期（20190715）">小程序开发月刊第七期（2019071...</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>29</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>27</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/自动化甜筒/" title="自动化甜筒">自动化甜筒<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>73</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
